package com.gosophia.dataDictionary.test.unit;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.hibernate.EntityMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.persister.entity.EntityPersister;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import com.gosophia.dataDictionary.entity.DataValue;
import com.gosophia.dataDictionary.entity.DataEntry;

@ContextConfiguration(locations = { "/applicationContext-map-test.xml" })
@Transactional
@TransactionConfiguration(defaultRollback = true)
public class UTHibernateMapping extends
        AbstractTransactionalJUnit4SpringContextTests {

    @Autowired
    private SessionFactory sessionFactory;

    @Test
    @SuppressWarnings("unchecked")
    public void allClassMapping() throws Exception {
        Session session = sessionFactory.getCurrentSession();
        Map metadata = sessionFactory.getAllClassMetadata();
        for (Object o : metadata.values()) {
            EntityPersister persister = (EntityPersister) o;
            Object e = generateEntity(persister.getMappedClass(EntityMode.POJO));
            session.save(e);
            session.flush();
            Serializable id = persister.getIdentifier(e, EntityMode.POJO);
            session.clear();
            session.get(persister.getMappedClass(EntityMode.POJO), id);
        }
    }

    @SuppressWarnings("unchecked")
    private Object generateEntity(Class entityClass) {
        if (entityClass.equals(DataValue.class)) {
            Session session = sessionFactory.getCurrentSession();
            DataEntry e = new DataEntry();
            e.setEntryName(UUID.randomUUID().toString().substring(0, 5));
            e.setEntryNameCN(UUID.randomUUID().toString().substring(0, 5));
            e.setEntryDescliption(UUID.randomUUID().toString().substring(0, 5));
            e.setEntryType("内置");
            session.save(e);
            session.flush();

            DataValue o = new DataValue();
            o.setOrderIndex(0);
            o.setIsEnabled(true);
            o.setValue(UUID.randomUUID().toString().substring(0, 5));
            o.setKey("001");

            List<DataValue> dataValues = new ArrayList<DataValue>();
            dataValues.add(o);
            e.setDataValues(dataValues);
            return o;
        } else if (entityClass.equals(DataEntry.class)) {

            DataEntry e = new DataEntry();
            e.setEntryName(UUID.randomUUID().toString().substring(0, 5));
            e.setEntryNameCN(UUID.randomUUID().toString().substring(0, 5));
            e.setEntryDescliption(UUID.randomUUID().toString().substring(0, 5));
            e.setEntryType("可定制");

            return e;
        } else {
            return null;
        }
    }
}
